#import "BWLayer.h"
#import "BWGridGeometry.h"


NSString *BWLayer_ChangeNotification = @"BWLayer Change Notification";

@implementation BWLayer


+ (void) initialize
{
    [BWLayer setVersion: 1];
} // initialize


- (id) init
{
    if (self = [super init]) {
        visible = YES;
        active = YES;
    }

    return (self);

} // init

- (void) dealloc
{
    [geometry release];
    [super dealloc];

} // dealloc


- (void) setName: (NSString *) string
{
    [name release];
    name = [string copy];
} // setName



- (NSString *) name
{
    return (name);
} // name


- (NSRect) bounds
{
    NSRect bounds;
    bounds.origin = NSZeroPoint;
    bounds.size.width = [geometry width];
    bounds.size.height = [geometry height];

    return (bounds);

} // bounds


- (void) setGeometry: (BWGridGeometry *) geo
{
    [geo retain];
    [geometry release];
    geometry = geo;

} // setGeometry


- (BWGridGeometry *) geometry
{
    return (geometry);
} // geometry



- (void) drawRect: (NSRect) rect
{
} // drawRect


- (void) setVisible: (BOOL) onOff
{
    [self willChangeValueForKey: @"visible"];
    visible = onOff;
    [self didChangeValueForKey: @"visible"];

    [self willChangeValueForKey: @"active"];
    [self didChangeValueForKey: @"active"];

} // setVisible


- (BOOL) visible
{
    return (visible);
} // visible


- (void) setActive: (BOOL) onOff
{
    [self willChangeValueForKey: @"active"];
    // can't be active and invisible

    if (onOff) {
        [self setVisible: YES];
    }

    active = onOff;
    [self didChangeValueForKey: @"active"];

} // setActive


- (BOOL) active
{
    // invisble layers aren't active either
    BOOL isActive = NO;

    if (visible) {
        isActive = active;
    }

    return (isActive);

} // active


- (BOOL) handleMouseDown: (NSEvent *) event
                 atPoint: (NSPoint) mouse
{
    return (NO);
} // handleMouseDown


- (void) handleMouseDragged: (NSEvent *) event
                    atPoint: (NSPoint) mouse
{
} // handleMouseDragged


- (void) handleMouseUp: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
} // handleMouseUp


- (void) postChangeNotification
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    [center postNotificationName: BWLayer_ChangeNotification
            object: self];

    [self willChangeValueForKey: @"layerChange"];
    [self didChangeValueForKey: @"layerChange"];

} // postChangeNotification


- (void) setNextHigherLayer: (BWLayer *) layer
{
    nextHigherLayer = layer;
} // setNextHigherLayer


- (BOOL) okToRenderRow: (int) row
                column: (int)column
{
    BOOL ret;
    if (nextHigherLayer != nil) {
        ret = [nextHigherLayer okToRenderRow: row
                               column: column];
    } else {
        ret = YES;
    } 
    return (ret);

} // okToRenderRow


- (BWLayerKind) layerKind
{
    return (kLayerKind_NoKind);
} // layerKind


- (void) encodeWithCoder: (NSCoder *) coder
{
    [coder encodeObject: name];
    [coder encodeObject: nextHigherLayer];
    
} // encodeWithCoder


- (id) initWithCoder: (NSCoder *) coder
{
    if (self = [super init]) {
        name = [coder decodeObject];
        nextHigherLayer = [coder decodeObject];

        [name retain];
        visible = YES;
        active = YES;
    }

    return (self);

} // initWithCoder


@end // BWLayer
